<?php
namespace Admin\Controller;
use Common\Controller\BasicController;
/**
 * 用户资料处理控制器
 */
class UserInfoController extends BasicController
{
  public function index(){
    $user = getLoginUserInfo();
    if(!$user['user_id']){
      $this->error('无法查到用户信息，请重新登录');
    }
    unset($user[password]);
    $this->assign('vo',$user);
    $this->display();
  }
  public function setStatus(){
    if(IS_POST){
      $data = I('post.');
      $id = $data['user_id'];
      unset($data['user_id']);
      $status = $data['status'];
      try{
        $res = D('User')->updateUser($id,$data);
        if($res === false){                 //tp save方法失败返回false，只有false的时候才是失败。
          return ajaxReturn(400,'修改失败');
        }else{
          return ajaxReturn(200,'修改成功');
        }
      }catch(Exception $e){
        return ajaxReturn(400,$e->getMessage());
      }
    }
  }
  public function save(){
    if(IS_POST){
      $data = I('post.');
      $id = $data['user_id'];
      $user = getLoginUserInfo();
      if(strcmp($user['username'],$data['username'])&&isset($data['username'])){
        return ajaxReturn(400,'非法操作');
      }
      if(strlen($data['password'])>0){
        if(strcmp($data['password'],$data['repassword'])!==0){
          return ajaxReturn(400,'两次输入密码不一致');
        }
      }
      unset($data['user_id']);
      unset($data['username']);
      try{
        $res = D('User')->updateUser($id,$data);
        if($res === false){                 //tp save方法失败返回false，只有false的时候才是失败。
          return ajaxReturn(400,'修改失败');
        }else{
          return ajaxReturn(200,'修改成功');
        }
      }catch(Exception $e){
          return ajaxReturn(400,$e->getMessage());
      }
    }
    }
    public function email(){
      if(IS_POST){
        $info = I('post.');
        if(!trim($info['email'])){
          return ajaxReturn(400,'邮箱不能为空');
        }
        // if(S('email_verify')){
        //   return ajaxReturn(400,'请一分钟后再试');
        // }
        $str = 'qwertyuiopasdfghklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890';
        for($i = 0;$i<=3;$i++){
          $code.= $str{mt_rand(0,strlen($str)-1)};
        }
        $code = strtolower($code);
        S('email_verify',$code,60);
        $res = sendEmail($info['email'],'测试邮件','邮箱激活验证码:'.$code.'1分钟内有效');
        var_dump($res);
        if($res ==true){
          return ajaxReturn(200,'验证码发送成功，请登录邮箱获取');
        }
      }
    }
}
